package com.zzz.bayer

import android.graphics.Bitmap

object GrayHelper {

    //平均值求灰度
    fun average(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map { pixel ->
            //计算灰度值
            val red = pixel and 0x00FF0000 shr 16
            val green = pixel and 0x0000FF00 shr 8
            val blue = pixel and 0x000000FF
            (red + green + blue) / 3
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //加权平均求灰度
    fun weighting(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map { pixel ->
            //计算灰度值
            val red = pixel and 0x00FF0000 shr 16
            val green = pixel and 0x0000FF00 shr 8
            val blue = pixel and 0x000000FF
            (red * 0.299 + green * 0.587 + blue * 0.114).toInt()
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //取最大值求灰度
    fun max(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map { pixel ->
            //计算灰度值
            val red = pixel and 0x00FF0000 shr 16
            val green = pixel and 0x0000FF00 shr 8
            val blue = pixel and 0x000000FF
            val array = arrayOf(red, green, blue)
            array.max()!!
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //取最小值求灰度
    fun min(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map {
            //计算灰度值
            val red = it and 0x00FF0000 shr 16
            val green = it and 0x0000FF00 shr 8
            val blue = it and 0x000000FF
            val array = arrayOf(red, green, blue)
            array.min()!!
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //最大最小平均值求灰度
    fun maxminAvg(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map {
            //计算灰度值
            val red = it and 0x00FF0000 shr 16
            val green = it and 0x0000FF00 shr 8
            val blue = it and 0x000000FF
            val array = arrayOf(red, green, blue)
            val min = array.min()!!
            val max = array.max()!!
            (min + max) / 2
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //红色通道求灰度
    fun red(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map {
            //计算灰度值
            it and 0x00FF0000 shr 16
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //绿色通道求灰度
    fun green(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map {
            it and 0x0000FF00 shr 8
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

    //蓝色通道求灰度
    fun blue(source: Bitmap): Bitmap {
        val width = source.width
        val height = source.height
        val sourceArray = IntArray(width * height)
        source.getPixels(sourceArray, 0, width, 0, 0, width, height)
        val resultArray = sourceArray.map {
            it and 0x000000FF
        }.map {
            //将8bit数据转为ARGB_8888
            0xFF000000.toInt() or (it shl 16) or (it shl 8) or it
        }.toIntArray()
        val resultBitmap = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        resultBitmap.setPixels(resultArray, 0, width, 0, 0, width, height)
        return resultBitmap
    }

}